package light.head.framework.utils;

import java.security.MessageDigest;

import sun.misc.BASE64Encoder;

public class MD5Utils {
	/**
	 * 24位MD5加密
	 * @param 要加密的字符串
	 * @return 加密结果
	 */
	public static String encryptTo24(String oldStr) {
		try {
			MessageDigest md5 = MessageDigest.getInstance("MD5");
			BASE64Encoder base64en = new BASE64Encoder();
			return base64en.encode(md5.digest(oldStr.getBytes("utf-8")));
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
	}

	/**
	 * 32位MD5加密
	 * @param			要加密的字符串
	 * @return			加密结果
	 */
	public static String encryptTo32(String oldStr) {
		try {
			MessageDigest digest = MessageDigest.getInstance("MD5");
			digest.update(oldStr.getBytes());
			byte hash[] = digest.digest();
			StringBuffer result = new StringBuffer(hash.length * 2);
			for (int i = 0; i < hash.length; i++) {
				if (((int) hash[i] & 0xff) < 0x10) {
					result.append("0");
				}
				result.append(Long.toString((int) hash[i] & 0xff, 16));
			}
			return result.toString();
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
}
